package com.yongninger.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.yongninger.activity.shipsments.ListActivity;
import com.yongninger.activity.user.ModifyPwdActivity;

public class MainActivity extends BaseActivity {
	SharedPreferences userInfo;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		userInfo = getSharedPreferences("user_info", 0);
		// 发货单区域
		Button shipsArea = (Button) this.findViewById(R.id.shipsArea);
		OnClickListener onClickListenerShipsArea = new OnClickListener() {
			@Override
			public void onClick(View v) {
				String umenu11 = userInfo.getString("umenu11", "");
				if("1".equals(umenu11)){
					Intent intent = new Intent(MainActivity.this,
							ListActivity.class);
					startActivity(intent);
				}else{
					Toast.makeText(getApplicationContext(), "您无权限浏览",
							Toast.LENGTH_SHORT).show();
				}
			}
		};
		shipsArea.setOnClickListener(onClickListenerShipsArea);
		// 仓库区域
		Button shipsinfoArea = (Button) this.findViewById(R.id.shipsinfoArea);
		OnClickListener onClickListenerShipsinfoArea = new OnClickListener() {
			@Override
			public void onClick(View v) {
				String umenu21 = userInfo.getString("umenu21", "");
				if("1".equals(umenu21)){
					Intent intent = new Intent(MainActivity.this,
							com.yongninger.activity.shipsmentinfo.ListActivity.class);
					startActivity(intent);
				}else{
					Toast.makeText(getApplicationContext(), "您无权限浏览",
							Toast.LENGTH_SHORT).show();
				}
			}
		};
		shipsinfoArea.setOnClickListener(onClickListenerShipsinfoArea);
		// 产品区域
		Button productArea = (Button) this.findViewById(R.id.productArea);
		OnClickListener onClickListenerProductArea = new OnClickListener() {
			@Override
			public void onClick(View v) {
				String umenu61 = userInfo.getString("umenu61", "");
				if("1".equals(umenu61)){
					Intent intent = new Intent(MainActivity.this,
							com.yongninger.activity.product.ListActivity.class);
					startActivity(intent);
				}else{
					Toast.makeText(getApplicationContext(), "您无权限浏览",
							Toast.LENGTH_SHORT).show();
				}
			}
		};
		productArea.setOnClickListener(onClickListenerProductArea);
		// 业务员区域
		Button clerkArea = (Button) this.findViewById(R.id.clerkArea);
		OnClickListener onClickListenerClerkArea = new OnClickListener() {
			@Override
			public void onClick(View v) {
				String umenu41 = userInfo.getString("umenu41", "");
				if("1".equals(umenu41)){
					Intent intent = new Intent(MainActivity.this,
							com.yongninger.activity.slerkinfo.ListActivity.class);
					startActivity(intent);
				}else{
					Toast.makeText(getApplicationContext(), "您无权限浏览",
							Toast.LENGTH_SHORT).show();
				}
			}
		};
		clerkArea.setOnClickListener(onClickListenerClerkArea);
		// 客户区域
		Button customerArea = (Button) this.findViewById(R.id.customerArea);
		OnClickListener onClickListenerCustomerArea = new OnClickListener() {
			@Override
			public void onClick(View v) {
				String umenu31 = userInfo.getString("umenu31", "");
				if("1".equals(umenu31)){
					Intent intent = new Intent(MainActivity.this,
							com.yongninger.activity.customer.ListActivity.class);
					startActivity(intent);
				}else{
					Toast.makeText(getApplicationContext(), "您无权限浏览",
							Toast.LENGTH_SHORT).show();
				}
			}
		};
		customerArea.setOnClickListener(onClickListenerCustomerArea);
		// 操作员区域
		Button userArea = (Button) this.findViewById(R.id.userArea);
		OnClickListener onClickListenerUserArea = new OnClickListener() {
			@Override
			public void onClick(View v) {
				String umenu51 = userInfo.getString("umenu51", "");
				if("1".equals(umenu51)){
					Intent intent = new Intent(MainActivity.this,
							com.yongninger.activity.user.ListActivity.class);
					startActivity(intent);
				}else{
					Toast.makeText(getApplicationContext(), "您无权限浏览",
							Toast.LENGTH_SHORT).show();
				}
			}
		};
		userArea.setOnClickListener(onClickListenerUserArea);
		// 修改密码
		Button modifyPwd = (Button) this.findViewById(R.id.modifyPwd);
		OnClickListener onClickListenerModifyPwd = new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this,
						ModifyPwdActivity.class);
				startActivity(intent);
			}
		};
		modifyPwd.setOnClickListener(onClickListenerModifyPwd);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			userInfo.edit().putString("upass", "").commit();
			userInfo.edit().putString("utype", "").commit();

			userInfo.edit().putString("umenu1", "").commit();
			userInfo.edit().putString("umenu11", "").commit();
			userInfo.edit().putString("umenu12", "").commit();

			userInfo.edit().putString("umenu2", "").commit();
			userInfo.edit().putString("umenu21", "").commit();
			userInfo.edit().putString("umenu22", "").commit();

			userInfo.edit().putString("umenu3", "").commit();
			userInfo.edit().putString("umenu31", "").commit();
			userInfo.edit().putString("umenu32", "").commit();

			userInfo.edit().putString("umenu4", "").commit();
			userInfo.edit().putString("umenu41", "").commit();
			userInfo.edit().putString("umenu42", "").commit();

			userInfo.edit().putString("umenu5", "").commit();
			userInfo.edit().putString("umenu51", "").commit();
			userInfo.edit().putString("umenu52", "").commit();
			finish();
			Intent intent = new Intent(MainActivity.this,
					com.yongninger.activity.LoginActivity.class);
			startActivity(intent);
			return true;
		}

		return super.onKeyDown(keyCode, event);
	}
}
